package org.ktm.web.bean;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.ktm.domain.KTMEntity;
import org.ktm.exception.KTMException;
import org.ktm.servlet.AbstractServlet;
import org.ktm.utils.Localizer;
import org.ktm.web.tags.Functions;

public abstract class FormBean {

	private String			uniqueId		= "";
	private String			method			= "";
	private String			module			= "";
	private Integer			pageNumber		= 1;
	private Integer			maxPage			= 5;
	private Integer			maxRows			= 0;
	private AbstractServlet	servlet			= null;

	private List<FormBean>	formCollection	= new ArrayList<FormBean>();

	public FormBean getBeanInstance()	throws ClassNotFoundException,
										InstantiationException,
										IllegalAccessException {
		FormBean bean = null;
		if ( servlet != null ) {
			Class<?> c = null;

			ClassLoader classLoader = Thread.currentThread()
					.getContextClassLoader();

			if ( classLoader != null ) {
				c = classLoader.loadClass( servlet.getBeanClass() );
			} else {
				c = Class.forName( servlet.getBeanClass() );
			}

			bean = (FormBean) c.newInstance();
			bean.setServlet( servlet );
		}
		return bean;
	}

	public void loadToForm( KTMEntity entity ) {
		if ( entity != null ) {
			this.setUniqueId( String.valueOf( entity.getUniqueId() ) );
		}
	}

	public void syncToEntity( KTMEntity entity ) {
		if ( entity != null ) {
			try {
				entity.setUniqueId( Integer.valueOf( this.getUniqueId() ) );
			}
			catch ( Exception e ) {

			}
		}
	}

	public void mergeEntity( KTMEntity fromEntity, KTMEntity toEntity ) {
		if ( fromEntity != null && toEntity != null ) {
			if ( fromEntity.getUniqueId() != null ) {
				toEntity.setUniqueId( fromEntity.getUniqueId() );
			}
		}
	}

	public void mergeForm( FormBean form ) {
		if ( form != null ) {
			if ( !Functions.isEmpty( form.getUniqueId() ) ) {
				this.setUniqueId( form.getUniqueId() );
			}
		}
	}

	public void loadFormCollection( Collection<?> entitys ) throws KTMException {
		throw new KTMException( "ERR_Not_Implement_need_override" );
	}

	public Collection<FormBean> getFormCollection() {
		return formCollection;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod( String method ) {
		this.method = method;
	}

	public String getModule() {
		return module;
	}

	public void setModule( String module ) {
		this.module = module;
	}

	public String getUniqueId() {
		return uniqueId;
	}

	public void setUniqueId( String uniqueId ) {
		this.uniqueId = uniqueId;
	}

	private Object [] getDefaultParameters( Method m ) {
		Class<?> [] clazzs = m.getParameterTypes();
		Object [] params = new Object[ clazzs.length ];

		for ( int i = 0; i < clazzs.length; i++ ) {
			Class<?> clazz = clazzs[ i ];
			String className = Localizer.getClassName( clazz );
			if ( className.equals( "Integer" ) || className.equals( "Double" )
					|| className.equals( "Float" )
					|| className.equals( "Byte" )
					|| className.equals( "Long" )
					|| className.equals( "Short" )
					|| className.equals( "Number" ) ) {
				params[ i ] = 0;
			} else if ( className.equals( "Boolean" ) ) {
				params[ i ] = Boolean.valueOf( "false" );
			} else {
				params[ i ] = String.valueOf( "" );
			}
		}
		return params;
	}

	protected boolean isSkipReset( String methodName ) {
		boolean result = false;
		// if (methodName != null) {
		// if (methodName.equals("setMethod")) {
		// result = true;
		// }
		// }
		return result;
	}

	public void reset() {
		Class<?> c = getClass();
		Method [] ms = c.getMethods();
		for ( Method m : ms ) {
			m.getParameterTypes();
			Object [] paramValues = getDefaultParameters( m );

			if ( isSkipReset( m.getName() ) ) {
				continue;
			}

			try {
				if ( m.getName().startsWith( "set" ) ) {
					m.invoke( this, paramValues );
				}
			}
			catch ( IllegalArgumentException e ) {

			}
			catch ( IllegalAccessException e ) {

			}
			catch ( InvocationTargetException e ) {

			}
			catch ( Exception e ) {

			}
		}
	}

	public AbstractServlet getServlet() {
		return servlet;
	}

	public void setServlet( AbstractServlet servlet ) {
		this.servlet = servlet;
	}

	public Double parseDouble( String value ) {
		Double result = 0.0;
		try {
			result = Double.parseDouble( value );
		}
		catch ( NumberFormatException nfe ) {
			nfe.printStackTrace();
		}
		return result;
	}

	@Override
	public int hashCode() {
		return uniqueId.hashCode();
	}

	@Override
	public boolean equals( Object obj ) {
		if ( this == obj ) return true;
		if ( obj == null ) return false;
		if ( getClass() != obj.getClass() ) return false;
		if ( obj instanceof FormBean ) {
			if ( uniqueId.equals( ( (FormBean) obj ).uniqueId ) ) {
				return true;
			}
		}
		return false;
	}

	public Integer getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber( Integer pageNumber ) {
		this.pageNumber = pageNumber;
	}

	public Integer getMaxRows() {
		return maxRows;
	}

	public void setMaxRows( Integer maxRows ) {
		this.maxRows = maxRows;
	}

	public Integer getMaxPage() {
		return maxPage;
	}

	public void setMaxPage( Integer maxPage ) {
		this.maxPage = maxPage;
	}

	public Integer getPageRange() {
		int range = pageNumber + ( maxPage - 1 );
		return ( range > maxRows ) ? maxRows : range;
	}
}
